package org.taishan.electronshop.servlet;

import org.taishan.electronshop.dao.*;
import org.taishan.electronshop.domain.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import static org.apache.commons.collections.CollectionUtils.index;

@WebServlet("/goods/*")
public class GoodsServlet extends HttpServlet {

    private TrolleyDao td = new TrolleyDao();
    private GoodsDao gd = new GoodsDao();
    private CustomerDao cd = new CustomerDao();
    private OrderDao od = new OrderDao();
    private OrderItemDao oid = new OrderItemDao();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession();
        String username = (String)session.getAttribute("username");
        System.out.println(username);
        if(username == null || username.length()<=0){
            response.sendRedirect("/index.vm");
            return;
        }

            Customer c = (Customer)session.getAttribute("customer");
        if(c == null){
            c=cd.load(username);
            session.setAttribute("customer",c);
        }
            Integer userid = c.getId();
            session.setAttribute("userid", userid);

        String uri = request.getRequestURI(); // 从 请求对象中获取 URI
        System.out.println("URI : " + uri);
        /**
         * uri : /employee           ========>  firstSlash == lastSlash
         * uri : /employee/         ========>   lastSlash == length -1
         * uri : /employee/xiaowen
         * uri : /employee/hezi/qing
         */
        int firstSlash = uri.indexOf("/"); // 获得第一个 / 对应的索引
        int lastSlash = uri.lastIndexOf("/"); // 获得最后一个 / 对应的索引
        int length = uri.length(); // 获得 字符串的长度
        int dotIndex = uri.lastIndexOf("."); // 从 uri 中获得最后一个 圆点的 索引

        // 如果最后一个斜杠不是第一个斜杠、并且最后一个斜杠不是最后一个字符、并且没有圆点
        if (firstSlash != lastSlash && lastSlash != length - 1 && dotIndex == -1) {
            String m = uri.substring(lastSlash + 1); // 截取 最后一个斜杠之后的内容
            System.out.println("最后一个 / 之后的内容:  " + m);
            m = m.toLowerCase(); // 全部转小写
            // JDK 1.5  之前 switch 中允许使用 byte 、short 、char、int ，从JDK 1.5 开始支持 枚举
            switch (m) { // Java 7 开始支持在  switch 中使用 String
                case "goods-buy":
                    goodsBuy(request, response);
                    break;
                default:
                    break;
            }
        }
    }


    /**
     *将这一行的购物车的数据，提交到订单，创建订单，和订单项，然后把购物车中的这项删除
     */
    protected void goodsBuy(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        int gid =Integer.parseInt(request.getParameter("gid"));
        double price =Double.parseDouble(request.getParameter("price"));
        Customer c =(Customer)session.getAttribute("customer");
        Integer uid = (Integer)session.getAttribute("userid");
        Goods g = gd.load(gid);
        TrolleyItem t = new TrolleyItem();
        t.setName(g.getName());
        t.setImage(g.getImage());
        t.setPrice(price);
        t.setGoods(g);
        t.setnumber(1);
        t.setCustomer(c);
        TrolleyItem tt = td.load(g.getName(),uid);
        if(tt !=null){
            int number = tt.getNumber();
            number = number +1;
            tt.setnumber(number);
            td.update(tt);
        }else {
            td.save(t);
        }
        response.sendRedirect("/trolley/trolley-items-list");
        return;
    }
}
